

#import <UIKit/UIKit.h>

#ifndef NSMInlines_h
#define NSMInlines_h

#pragma mark - UIColor
static inline UIColor *RGBAColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a) {
    return [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a/1.0f];
}

static inline UIColor *RGBColor(CGFloat r, CGFloat g, CGFloat b) {
    return RGBAColor(r, g, b, 1);
}

static inline UIColor *WhiteColor(CGFloat w, CGFloat a) {
    return [UIColor colorWithWhite:w/1.0f alpha:a/1.0f];
}

static inline UIColor *RandomAColor(CGFloat a) {
    return RGBAColor(arc4random()%255, arc4random()%255, arc4random()%255, a);
}

static inline UIColor *RandomColor() {
    return RandomAColor(1);
}

#pragma mark - SafeObject
static inline BOOL SafeObject(id obj) {
    return obj && ![obj isEqual:NSNull.null];
}

static inline NSString *SafeString(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSString.class];
    return isValid? obj: @"";
}

static inline NSArray *SafeArray(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSArray.class];
    return isValid? (NSArray *)obj: @[];
}

static inline NSDictionary *SafeDictionary(id obj) {
    BOOL isValid = SafeObject(obj) && [obj isKindOfClass:NSDictionary.class];
    return isValid? (NSDictionary *)obj: @{};
}

#pragma mark - NSString
static inline NSString *IntegerString(NSInteger v) {
    return [NSString stringWithFormat:@"%@", @(v)];
}

static inline NSString *ReplaceString(id obj, NSString *str) {
    NSString *str1 = SafeString(obj);
    NSString *str2 = SafeString(str);
    return str1.length? str1: str2;
}

#pragma mark - UIImage
static inline UIImage *NameImage(NSString *name) {
    return [UIImage imageNamed:name];
}

static inline UIImage *FileImage(NSString *name, NSString *type) {
    return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:type]];
}

static inline UIImage *OriginalImage(NSString *name) {
    return [NameImage(name) imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

static inline UIImage *TemplateImage(NSString *name) {
    return [NameImage(name) imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}

static inline UIFont *CustomFont(CGFloat fontSize) {
    return [UIFont fontWithName:@"STHeitiSC-Light" size:fontSize];
}

static inline UIFont *NumericFontBlack(CGFloat fontSize) {
    return [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:fontSize];
}

static inline UIFont *NumericFontBold(CGFloat fontSize) {
    return [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:fontSize];
}

static inline UIFont *PhoneFontBlack(CGFloat fontSize) {
    return [UIFont fontWithName:@"Optima-ExtraBlack" size:fontSize];
}

static inline UIFont *PhoneFontBold(CGFloat fontSize) {
    return [UIFont fontWithName:@"Optima-Bold" size:fontSize];
}

static inline UIFont *SystemFont(CGFloat fontSize) {
    return [UIFont systemFontOfSize:fontSize];
}

static inline UIFont *BoldSystemFont(CGFloat fontSize) {
    return [UIFont boldSystemFontOfSize:fontSize];
}

#pragma mark - CG
static inline BOOL CGSizeIsEmpty(CGSize size) {
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    return CGRectIsEmpty(rect);
}

static inline CGPoint CGSizeGetCenterPoint(CGSize size) {
    return CGPointMake(size.width/2, size.height/2);
}

static inline CGFloat CGFloatGetMid(CGFloat f1, CGFloat f2) {
    return (f1 - f2) / 2.0;
}

static inline CGFloat UIEdgeInsetsGetWidth(UIEdgeInsets insets) {
    return insets.left + insets.right;
}

static inline CGFloat UIEdgeInsetsGetHeight(UIEdgeInsets insets) {
    return insets.top + insets.bottom;
}

static inline CGRect CGRectIntersectsEdgeInsets(CGRect rect, UIEdgeInsets insets) {
    rect.origin.x += insets.left;
    rect.origin.y += insets.top;
    rect.size.width -= UIEdgeInsetsGetWidth(insets);
    rect.size.height -= UIEdgeInsetsGetHeight(insets);
    return rect;
}

#pragma mark - bar
static inline CGFloat StatusBarHeight() {
    return UIApplication.sharedApplication.statusBarFrame.size.height;
}

static inline CGFloat NavBarHeight() {
    return  44.0 + StatusBarHeight();
}

static inline CGFloat TabBarHeight() {
    return  StatusBarHeight()>20? 83: 49;
}

#pragma mark - bundle
static inline NSDictionary *MainBundleDictionary() {
    return SafeDictionary(NSBundle.mainBundle.infoDictionary);
}

static inline NSString *AppBundleName() {
    return MainBundleDictionary()[@"CFBundleName"];
}

static inline NSString *AppBundleID() {
    return MainBundleDictionary()[@"CFBundleIdentifier"];
}
static inline NSString *AppBuild() {
    return MainBundleDictionary()[@"CFBundleVersion"];
}

static inline NSString *AppVersion() {
    return MainBundleDictionary()[@"CFBundleShortVersionString"];
}

static inline id InfoPlistValueForKey(NSString *key) {
    return MainBundleDictionary()[SafeString(key)];
}

#define kNullObject             ([NSNull null])

#define kAppBundleName          AppBundleName()
#define kAppBundleID            AppBundleID()
#define kAppBuild               AppBuild()
#define kAppVersion             AppVersion()

#define kStatusBarHeight        StatusBarHeight()
#define kNavBarHeight           NavBarHeight()
#define kTabBarHeight           TabBarHeight()
#define kScreenBounds           (UIScreen.mainScreen.bounds)

#endif /* NSMInlines_h */
